package controllers
{
    import com.greensock.events.LoaderEvent;
    import com.greensock.loading.ImageLoader;
    import com.greensock.loading.LoaderMax;
    import com.greensock.loading.MP3Loader;
    import com.pamakids.manager.LoadManager;
    import com.pamakids.models.ResultVO;
    import com.pamakids.utils.CloneUtil;
    
    import flash.display.BitmapData;
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.media.Sound;
    import flash.utils.Dictionary;
    
    import models.PaintData;
    import models.UserVO;
    import models.VO;
    
    import proxy.FileProxy;
    
    public class AlbumLoader extends EventDispatcher
    {
        public function AlbumLoader(_aid:String)
        {
            API.instance.getAlbumDetail(detailCB,_aid,UserVO.crtUser._id);
        }
        
        private var paintArr:Array;
        
        private function detailCB(o:ResultVO):void
        {
            if(o.status)
            {
                paintArr = o.results as Array;
                doDownload();
            }else
            {
                dispatchEvent(new ErrorEvent(ErrorEvent.ERROR));
            }
        }
        
        private function doDownload():void
        {
            for (var i:int = 0; i < paintArr.length; i++) 
            {
                var paint:Object = paintArr[i];
                loaderDic[paint._id] = new LoaderMax();
            }
        }
        
        private var loaderDic:Dictionary = new Dictionary();
        private var queue:LoaderMax;
        
        public function init():void
        {
            if(paint.data)
                LoadManager.instance.loadText(paint.dataUrl,onLoadDataComplete);
            else 
                dispatchEvent(new ErrorEvent(ErrorEvent.ERROR));
            
            _queue = new LoaderMax({name:"childQueue",onComplete:completeHandler,onError:errorHandler,
                    onProgress:progressFunction,maxConnections:3});
        }
        
        private function errorHandler(event:LoaderEvent):void
        {
            dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,event.text));
        }
        
        public var paintData:PaintData;
        
        private function onLoadDataComplete(s:String):void
        {
            if(s.indexOf('replayData')>=0)
            {
                onLoadAnimeDataComplete(s);
            }else
            {
                paintData = FileProxy.parseConfig(s);
                var tarr:Array=paintData.getTextureArr();
                
                for (var i:int = 0; i < tarr.length; i++) 
                {
                    _queue.append( new ImageLoader(VO.FILESERVER_HOST+tarr[i], {}) );
                }
                _queue.load();
            }
        }
        
        private function onLoadAnimeDataComplete(s:String):void
        {
            s=s.substr(s.indexOf("{"));
            animeData=CloneUtil.convertObject(JSON.parse(s),AnimeData);
            var tarr:Array=animeData.textures;
            for (var i:int = 0; i < tarr.length; i++) 
            {
                _queue.append( new ImageLoader(VO.FILESERVER_HOST+tarr[i], {name:tarr[i]}) );
            }
            
            coverUrl=animeData.background;
            _queue.append(new ImageLoader(VO.FILESERVER_HOST+coverUrl,{name:coverUrl}));
            
            if(paint.audio)
            {
                sndUrl=paint.audio;
                if(sndUrl.indexOf(VO.AUDIO_NAME_AMR)>0)
                    sndUrl+=VO.AUDIO_MP3;
                _queue.append(new MP3Loader(VO.FILESERVER_HOST+sndUrl,{name:sndUrl,autoPlay:false}));
            }
            
            _queue.load();
        }
        
        public function dispose():void
        {
            if(_queue)
                _queue.dispose(true);
            _queue=null;
        }
        
        public var animeData:AnimeData;
        
        private var sndUrl:String;
        private var coverUrl:String;
        
        private function completeHandler(event:LoaderEvent):void 
        {
            dispatchEvent(new Event(Event.COMPLETE));
        }
        
        private function progressFunction(event:LoaderEvent):void 
        {
            _queue.progress;
        }
        
        public function getBitmapByName(name:String):BitmapData
        {
            var obj:Object=_queue.getContent(name);
            return obj.rawContent.bitmapData.clone();
        }
        
        public function getBG():BitmapData
        {
            return getBitmapByName(coverUrl);
        }
        
        public function getMP3():Sound
        {
            if(sndUrl)
                return _queue.getContent(sndUrl);
            return null;
        }
    
    }
}

